package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class TrainClient {


	public String updateTrainCC(String cc) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", cc);			
		try {
			String resultXml = WSUtils.callWS("getCcTrain",params, Constants.ToolURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}


	
	
	public List<Map<String,Object>> pullCCXml(String resultXml) throws Exception {
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else if(resultXml.equals("N")){
			throw new Exception("无查询信息！");
		}else{
			List<Map<String,Object>> list=null;
			Map<String, Object> map =null;			
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {				
					switch (event) {
						case XmlPullParser.START_DOCUMENT:							
							list=new ArrayList<Map<String,Object>>();
							break;
						case XmlPullParser.START_TAG:
							if ("item".equals(parser.getName())) {
								map = new HashMap<String, Object>();
							}
							if (map != null) {
								if ("num".equals(parser.getName())) {
									map.put("num", parser.nextText());
								} else if ("city".equals(parser.getName())) {
									map.put("city", parser.nextText());
								}else if ("firstDate".equals(parser.getName())) {
									map.put("firstDate", parser.nextText());
								} else if ("lastDate".equals(parser.getName())) {
									map.put("lastDate", parser.nextText());
								} else if ("date".equals(parser.getName())) {
									map.put("date", parser.nextText());
								} 							 
							}
							break;
						case XmlPullParser.END_TAG:
							if ("item".equals(parser.getName())) {							
								list.add(map);						
								map = null;						
							}
							break;
						}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}			
			return list;
		}
		
	}
	
	
	public String updateTrainMDD(String sfd,String mdd) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", sfd);		
		params.put("arg1", mdd);		
		try {
			String resultXml = WSUtils.callWS("getMddTrain",params, Constants.ToolURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}


	
	
	public List<Map<String,Object>> pullMDDXml(String resultXml) throws Exception {
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else if(resultXml.equals("N")){
			throw new Exception("无查询信息！");
		}else{
			List<Map<String,Object>> list=null;
			Map<String, Object> map =null;		
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {				
					switch (event) {
						case XmlPullParser.START_DOCUMENT:						
							list=new ArrayList<Map<String,Object>>();
							break;
						case XmlPullParser.START_TAG:
							if ("item".equals(parser.getName())) {
								map = new HashMap<String, Object>();
							}
							if (map != null) {
								if ("cc".equals(parser.getName())) {
									map.put("cc", parser.nextText());
								} else if ("firstDate".equals(parser.getName())) {
									map.put("firstDate", parser.nextText());
								} else if ("lastDate".equals(parser.getName())) {
									map.put("lastDate", parser.nextText());
								} 						 
							}
							break;
						case XmlPullParser.END_TAG:
							if ("item".equals(parser.getName())) {							
								list.add(map);						
								map = null;						
							}
							break;
						}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}		
			return list;
		}
		}
}
